package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.*;
import com.taotao.service.ItemService;
import com.taotao.utils.IDUtils;
import com.taotao.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;
    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;

	@Override
	public TbItem getItemById(long itemId) { 
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		return item;
	}

    @Override
    public EasyUIDataGridResult getItemList(int page,int rows) {
        //分页处理
        PageHelper.startPage(page, rows);
        //执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = itemMapper.selectByExample(example);
        //取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        //返回处理结果
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);

        return result;
    }

    /**
     * 商品新增
     * @param item
     * @param desc
     * @return
     */
    @Override
    public TaotaoResult createItem(TbItem item, String desc,String itemParams) {
        long itemId = IDUtils.genItemId();
        item.setId(itemId);
        // '商品状态，1-正常，2-下架，3-删除'
        item.setStatus((byte) 1);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        itemMapper.insert(item);
        //新增商品描述信息
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(new Date());
        itemDesc.setUpdated(new Date());
        itemDescMapper.insert(itemDesc);
        //新增规格参数信息
        TbItemParamItem itemParamItem = new TbItemParamItem();
        itemParamItem.setItemId(itemId);
        itemParamItem.setParamData(itemParams);
        itemParamItem.setCreated(new Date());
        itemParamItem.setUpdated(new Date());
        itemParamItemMapper.insert(itemParamItem);
        return TaotaoResult.ok();
    }

    @Override
    public String getItemParamHtml(Long itemId) {
        TbItemParamItemExample itemParamItemExample = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = itemParamItemExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = itemParamItemMapper.selectByExampleWithBLOBs(itemParamItemExample);

        if (list == null || list.isEmpty()) {
            return"";
        }
        TbItemParamItem itemParamItem = list.get(0);
        String paramData = itemParamItem.getParamData();
        List<Map> mapList = JsonUtils.jsonToList(paramData,  Map.class);
        StringBuffer sb = new StringBuffer();

        sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"1\" class=\"Ptable\">\n");
        sb.append("	<tbody>\n");
        for (Map map : mapList) {
            sb.append("		<tr>\n");
            sb.append("			<th class=\"tdTitle\" colspan=\"2\">"+map.get("group")+"</th>\n");
            sb.append("		</tr>\n");
            //取规格项
            List<Map>mapList2 = (List<Map>) map.get("params");
            for (Map map2 : mapList2) {
                sb.append("		<tr>\n");
                sb.append("			<td class=\"tdTitle\">"+map2.get("k")+"</td>\n");
                sb.append("			<td>"+map2.get("v")+"</td>\n");
                sb.append("		</tr>\n");
            }
        }
        sb.append("	</tbody>\n");
        sb.append("</table>");

        return sb.toString();
    }

    @Override
    public TaotaoResult updateItem(TbItem item, String desc, String itemParams,Long itemParamId) {
        //更新商品表
        item.setUpdated(new Date());
        item.setCreated(new Date());
        item.setStatus((byte)1);
        itemMapper.updateByPrimaryKey(item);
        //更新商品描述信息
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(item.getId());
        itemDesc.setItemDesc(desc);
        itemDesc.setUpdated(new Date());
        itemDesc.setCreated(new Date());
        itemDescMapper.updateByPrimaryKey(itemDesc);
        //更新模板参数信息
        TbItemParamItemExample itemExample = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = itemExample.createCriteria();
        criteria.andItemIdEqualTo(item.getId());
        List<TbItemParamItem> list = itemParamItemMapper.selectByExample(itemExample);
        if (list != null && list.size()>0){
            TbItemParamItem itemParamItem = list.get(0);
            itemParamItem.setParamData(itemParams);
            itemParamItem.setCreated(new Date());
            itemParamItem.setUpdated(new Date());
            itemParamItemMapper.updateByPrimaryKey(itemParamItem);
            return TaotaoResult.ok(itemParamItem);
        }
        return TaotaoResult.ok();
    }

    @Override
    public TbItemDesc getItemByItemid(Long itemid) {
        TbItemDescExample example = new TbItemDescExample();
        TbItemDescExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemid);
        List<TbItemDesc> list = itemDescMapper.selectByExampleWithBLOBs(example);
        TbItemDesc desc = list.get(0);
        return desc;
    }

    @Override
    public TaotaoResult getItemCatByItemid(Long itemid) {
        TbItemParamItemExample example = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemid);
        List<TbItemParamItem> list = itemParamItemMapper.selectByExampleWithBLOBs(example);
        if (list != null && list.size() > 0){
            TbItemParamItem itemParamItem = list.get(0);
            return TaotaoResult.ok(itemParamItem);
        }
        return TaotaoResult.ok();
    }

}
